package com.bytecamp.testplatform.controller;

import com.bytecamp.testplatform.dto.BaseUrlDTO;
import com.bytecamp.testplatform.model.TestResult;
import com.bytecamp.testplatform.service.TestResultService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/results")
@Tag(name = "测试结果管理", description = "执行测试用例并查询结果")
@RequiredArgsConstructor
public class TestResultController {

    private final TestResultService testResultService;

    // 执行单个用例（接收baseUrl）
    @PostMapping("/run/{caseId}")
    public String runSingleCase(
            @PathVariable Long caseId,
            @RequestBody BaseUrlDTO baseUrlDTO) { // 接收前端传递的baseUrl
        return testResultService.executeAndSave(caseId, baseUrlDTO.getBaseUrl());
    }

    // 批量执行用例（接收baseUrl）
    @PostMapping("/run/batch/{apiId}")
    public String runBatchCases(
            @PathVariable Long apiId,
            @RequestBody BaseUrlDTO baseUrlDTO) {
        return testResultService.batchExecuteAndSave(apiId, baseUrlDTO.getBaseUrl());
    }

    /**
     * 查询测试结果
     */
    @GetMapping("/list")
    @Operation(summary = "查询测试结果", description = "可按用例ID或接口ID筛选，默认查询所有")
    public List<TestResult> getResultList(
            @RequestParam(required = false) Long caseId,
            @RequestParam(required = false) Long apiId) {
        return testResultService.getResults(caseId, apiId);
    }
}